package com.buding.fx.common.constant;

import com.buding.fx.common.enums.PropertyKeyEnum;
import com.buding.fx.dao.PropertyDao;
import com.buding.fx.dao.impl.PropertyDaoImpl;
import com.buding.fx.entity.Property;
import com.sun.javafx.tk.Toolkit;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import lombok.Data;

import java.awt.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mingdongwei-jwk
 * @date 2022/09/14 17:05
 * @class com.buding.fx.entity
 * @Description
 */
@Data
public class CssStyleInfo {
    private double  total_percent = 0.6;

    private    double HEIGHT = 800; //整体高

    private   double WIGTH = 1200;

    private   double top_height_percent = 0.15;   //顶部菜单总体占高0.3

    private   double top_wight = WIGTH; //顶部导航栏宽
    private   double top_height= HEIGHT * top_height_percent; //导航栏占整体高度（包含导航栏）

    private   double  top_logo_line_percent = 0.6;  //顶部菜单，logo部分占比

    private   double top_logo_line_width =WIGTH;   //宽和整体对其
    private   double top_logo_line_height = top_height*top_logo_line_percent;  //顶部高度减去百分比

    private   double top_navigation_bar_width = top_wight;    //顶部导航宽
    private   double top_navigation_bar_height = top_height-top_logo_line_height;

    private   double left_width_percent = 0.3;   //左边整体占宽的0。3
    private   double left_wight = WIGTH * left_width_percent;    //左边树宽
    private   double left_height = HEIGHT-top_height; //左边树高

    private   double center_weight = WIGTH-left_wight;
    private   double center_height = HEIGHT-top_height;

    private   double center_content_left = 10;  //中间内容距离左边边框的距离

    private   double center_title_height = 30;   //中间区域导航栏的高度

    private   int navigation_font_size = 15;    //默认点击字体大小
    private   int onclick_navigation_font_size = 16;  // 点击以后字体大小

    private  double top_icon_img_width  = 25;  //默认图片展示区域宽度
    private  double onclick_icon_width  = 30;  //点击放大后宽度

    private String navigation_backhround_color = "#3a4041";  //导航栏背景颜色

    private String navigation_font_color = "#EAE2E2FF";  //默认字体颜色

    private String navigation_font_click_color = "#FFFFFFFF";  // 电子字体颜色


    private double icon_gap = 50;  //icon图片间隔距离

    private double centr_serch_width_percent  =  0.8;    //中间部分搜索框占center宽度的百分比
    private double centr_serch_width = center_weight*centr_serch_width_percent;

    private String navigation_font_famliy = "Arial Rounded MT Bold";

    private String left_node_right_border_color = "#d19b5e";

    private  String logoUrl  =  "img/icon/logo001.png";

    private  String systemName = "超级记事本";

    private String system_name_font_color = "#588122";

    private String system_name_font_shadow_color = "#5795cf";

    private String system_name_font_family = "Franklin Gothic Demi Cond";

    private  String version  = "V1.0.1";

    private String center_search_background_color = "#defcf9";


    /**
     * 重新计算整体长宽宽并且落库
     * @param total_percent 总体占电脑分辨率比
     * @param top_height_percent  顶部占比
     * @param top_logo_line_percent  顶部logo高度占比
     * @param left_width_percent 左边部分占比总体宽
     * @return
     */
    public CssStyleInfo resize(double total_percent,double top_height_percent,double top_logo_line_percent,double left_width_percent){
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        this.WIGTH = gd.getDisplayMode().getWidth()*total_percent; //获取到绝对宽度
        this.HEIGHT = gd.getDisplayMode().getHeight()*total_percent; // 获取到绝对高度*/
        /*Rectangle2D screenRectangle = Screen.getPrimary().getBounds();
        this.WIGTH = screenRectangle.getWidth()*total_percent;  //获取相对宽度
        this.HEIGHT = screenRectangle.getHeight()*total_percent; //获取相对高度*/
        System.out.println("system WIGTH: "+WIGTH+";  system HEIGHT: "+HEIGHT);
        this.top_height_percent =top_height_percent;   //顶部菜单总体占高0.3

        this.top_wight = WIGTH; //顶部导航栏宽
        this.top_height= HEIGHT * top_height_percent; //导航栏占整体高度（包含导航栏）

        this.top_logo_line_percent = top_logo_line_percent;  //顶部菜单，logo部分占比

        this.top_logo_line_width =WIGTH;   //宽和整体对其
        this.top_logo_line_height = top_height*top_logo_line_percent;  //顶部高度减去百分比

        this.top_navigation_bar_width = top_wight;    //顶部导航宽
        this.top_navigation_bar_height = top_height-top_logo_line_height;

        this.left_width_percent = left_width_percent;   //左边整体占宽的0。3
        this.left_wight = WIGTH * left_width_percent;    //左边树宽
        this.left_height = HEIGHT-top_height; //左边树高

        this.center_weight = WIGTH-left_wight;
        this.center_height = HEIGHT-top_height;
        return this;
    }

    /**
     * 重新初始化窗口大小并且入库
     * @param total_percent
     * @param top_height_percent
     * @param top_logo_line_percent
     * @param left_width_percent
     * @return
     */
    public CssStyleInfo forResize(double total_percent,double top_height_percent,double top_logo_line_percent,double left_width_percent){
        CssStyleInfo cssStyleInfo = new CssStyleInfo();
        cssStyleInfo.resize( total_percent, top_height_percent, top_logo_line_percent, left_width_percent);
        try{
            PropertyDao propertyDao = new PropertyDaoImpl();
            List<Property> lists = propertyDao.selectAll();
            Map<String,Property> pMap = lists.stream().collect(Collectors.toMap(Property::getKey, property -> property));
            Field[] declaredFields = cssStyleInfo.getClass().getDeclaredFields();
            for (Field field:declaredFields){
                String name = field.getName();
                Property property = pMap.get(name);
                String newValue = field.get(cssStyleInfo).toString();
                if (property!=null){
                    if (!property.getValue().equals(newValue)){
                        property.setValue(newValue.toString());
                        property.setUpdateBy("sys");
                        propertyDao.update(property);
                    }
                }else{
                    Property insertPro = new Property();
                    insertPro.setKey(name);
                    insertPro.setValue(newValue);
                    insertPro.setDesc(PropertyKeyEnum.getDescByName(name));
                    insertPro.setDisable(0);
                    propertyDao.insert(insertPro);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return cssStyleInfo;
    }

    /**
     * 此处main方法是直接将此前窗口属性插入数据库（请确保数据库sys_property为空）
     * @param args
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws IllegalAccessException {
        CssStyleInfo css = new CssStyleInfo();
        css.resize(0.6,0.15,0.55,0.2);
        Map<String,String> filedMap = new HashMap<>();
        Field[] declaredFields = css.getClass().getDeclaredFields();
        for (Field field:declaredFields){
            String name = field.getName();
            Object o = field.get(css);
            filedMap.put(name,o.toString());
        }
        System.out.println("CssStyleInfo.main: "+filedMap.toString());
        PropertyDao propertyDao = new PropertyDaoImpl();
        for (PropertyKeyEnum propertyKeyEnum:PropertyKeyEnum.values()){
            String key = propertyKeyEnum.name();
            String value = filedMap.get(key);
            Integer disable = 0;
            String desc = propertyKeyEnum.getDesc();
            Property property = new Property();
            property.setKey(key);
            property.setDesc(desc);
            property.setValue(value);
            property.setDisable(disable);
            propertyDao.insert(property);
        }

    }

}
